package com.qyhl.module_practice.search;


import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeSearchBean;

public class PracticeSearchModel implements PracticeSearchContract.PracticeSearchModel {
    private PracticeSearchPresenter mPresenter;

    PracticeSearchModel(PracticeSearchPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void doSearch(String key) {
        EasyHttp.get(PracticeUrl.GET_SEARCH_RESULT)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("key", key)
                .execute(new SimpleCallBack<PracticeSearchBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.searchError("暂无相关内容！");
                        } else {
                            mPresenter.searchError("搜索出错，请重新尝试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeSearchBean practiceSearchBean) {
                        if (practiceSearchBean != null &&
                                ((practiceSearchBean.getAct() != null && practiceSearchBean.getAct().size() > 0)
                                        || (practiceSearchBean.getInst() != null && practiceSearchBean.getInst().size() > 0))) {
                            mPresenter.setResult(practiceSearchBean);
                        } else {
                            mPresenter.searchError("暂无相关内容！");
                        }
                    }
                });

    }
}
